package com.htfyun.ft.fragment.factory;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.htfyun.ft.R;
import com.htfyun.hardware.LedRGBController;

import androidx.annotation.Nullable;

public class FragmentLedRGB extends FactoryBaseFragment implements CompoundButton.OnCheckedChangeListener {

    private Switch switchRed;
    private Switch switchGreen;
    private Switch switchBlue;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_factory_led_rgb;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);

        switchRed = (Switch) findViewById(R.id.switchRed);
        switchGreen = (Switch) findViewById(R.id.switchGreen);
        switchBlue = (Switch) findViewById(R.id.switchBlue);

    }

    @Override
    protected void onVisible() {
        super.onVisible();

        switchRed.setOnCheckedChangeListener(this);
        switchGreen.setOnCheckedChangeListener(this);
        switchBlue.setOnCheckedChangeListener(this);

    }

    @Override
    protected void onInvisible() {
        super.onInvisible();

        switchRed.setOnCheckedChangeListener(null);
        switchGreen.setOnCheckedChangeListener(null);
        switchBlue.setOnCheckedChangeListener(null);

    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        LedRGBController.LedRGBAttr attr = null;

        if (buttonView == switchRed) {
            attr = LedRGBController.LedRGBAttr.red;
        } else if (buttonView == switchGreen) {
            attr = LedRGBController.LedRGBAttr.green;
        } else if (buttonView == switchBlue) {
            attr = LedRGBController.LedRGBAttr.blue;
        }

        if (attr != null) {
            LedRGBController.setLedOn(attr, isChecked);
        }

    }
}
